package com.example.springsecurity.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.example.springsecurity.mapper.UserMapper;
import com.example.springsecurity.pojo.LoginVO;
import com.example.springsecurity.pojo.MyUser;
import com.example.springsecurity.pojo.MyUserDetails;
import com.example.springsecurity.pojo.Result;
import com.example.springsecurity.service.LoginService;
import com.example.springsecurity.service.UserDetailsServiceImpl;
import com.example.springsecurity.utils.SMSUtil;
import jakarta.annotation.Resource;
import org.assertj.core.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;
@CrossOrigin//跨域
@RestController
public class LoginController {//这个是安全的禁用跨域接口
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private LoginService loginService;

    @Autowired
    private SMSUtil smsUtil;

    @Autowired
    private UserMapper userMapper;



    @PostMapping("/login")
    public Result login(@RequestBody LoginVO loginVO) {
        return loginService.login(loginVO);
    }

    @RequestMapping("/test")
    public String test(){
        return "测试";
    }

 /*
 * 获取图形验证码接口*/


    @RequestMapping("/cap")
    public Result cap() {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 10);
        // 获取验证码
        String code = lineCaptcha.getCode();
        //redis存储验证码
        redisTemplate.opsForValue().set("code", code);//以key，value形式存储到redis中
        //设置验证码在redis中的有效期为30s
        redisTemplate.expire("code", 20, TimeUnit.SECONDS);
        //返回一个base64编码的图片json字符串
        String imageBase64Data = lineCaptcha.getImageBase64Data();

        return new Result(200, "成功获取验证码", imageBase64Data);
    }




    @PostMapping("/sendSms")
    public Result sendSms(@RequestBody Map<String,String> map) {
        String phone = map.get("phone");
        String sms = smsUtil.generateSms(phone);//得到验证码
        Integer code=500;
        String message="发送失败";
        Object data=null;
        if(sms != null) {
            code=200;
            message="发送成功";
            data=sms;

        }
        return new Result(code,message,data);
    }




    @PostMapping("/register")
    public Result register( String username, String password, String sms, String phone){

        //根据用户名查询用户
        MyUser myUser = userMapper.getUserByUsername(username);
        //验证短信验证码sms是否正确
        boolean smsIstrue = smsUtil.validateSms(phone,sms);//这块的问题！！
        if(smsIstrue==false){
            return new Result(401,"验证码错误",null);
        }
        if(myUser==null&&smsIstrue){
            //未查询到，则用户名不重复，并且验证码正确，可以注册
            //userService.register(username,password);
            MyUser addUser = new MyUser();
            addUser.setUsername(username);
            addUser.setPassword(password);
            userMapper.insertUser(addUser);
            return new Result(0,"ok",null);
        }else {
            //查询到，用户名重复，不可注册
            return new Result(1,"用户名重复",null);
        }
    }





}
